package org.jeecg.modules.arc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.YouBianCodeUtil;
import org.jeecg.modules.arc.entity.BasePeriod;
import org.jeecg.modules.arc.mapper.BasePeriodMapper;
import org.jeecg.modules.arc.service.IBasePeriodService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;

/**
 * @Description: 会计期间
 * @Author: jeecg-boot
 * @Date:   2022-02-10
 * @Version: V1.0
 */
@Service
public class BasePeriodServiceImpl extends ServiceImpl<BasePeriodMapper, BasePeriod> implements IBasePeriodService {

    //扎帐
    @Override
    @Transactional
    public void tieUp(BasePeriod basePeriod) {
        BasePeriod basePeriod1 = new BasePeriod();
        BeanUtils.copyProperties(basePeriod,basePeriod1);

        basePeriod1.setId(null);
        //String nextMonth = YouBianCodeUtil.getNextBianCode(null, basePeriod1.getMonth(), 6);
        //basePeriod1.setMonth(nextMonth);
        basePeriod.setIsUsed(0);
        baseMapper.updateById(basePeriod);

        long btime = basePeriod1.getBeginDate().getTime();
        long etime = basePeriod1.getEndDate().getTime();
        Calendar bcalendar = DateUtils.getCalendar(btime);
        bcalendar.add(Calendar.MONTH,1);
        basePeriod1.setBeginDate(bcalendar.getTime());

        Calendar ecalendar = DateUtils.getCalendar(etime);
        ecalendar.add(Calendar.MONTH,1);
        basePeriod1.setEndDate(null);

        String nextMonth = DateUtils.formatDate(basePeriod1.getBeginDate(), "yyyyMM");
        basePeriod1.setMonth(nextMonth);

        basePeriod1.setIsUsed(1);
        baseMapper.insert(basePeriod1);

    }
}
